#include "ce1.h"
#include "gtest/gtest.h"
#include "Tester.h"

void TextBuddy::SetName(string fileName1)
{	
	fileName=fileName1;
	if(fileName.substr(fileName.size()-4)==extension)
	{}
	else
	{
		fileName+=extension;
	}

}

void TextBuddy::DisplayHeaderText()
{
	cout<<welcome<<fileName<<isReady<<endl<<endl;
}



void TextBuddy::Add(string addend)
{	
	cout<<endl;
	cout<<addedTo<<fileName<<":"<<addend<<endl<<endl;
	noSentences++;
	paragraph.push_back(addend);

}

void TextBuddy::Clear()
{		
	cout<<endl;
	cout<<contentDeleted<< fileName<<endl<<endl;
	paragraph.clear();
	ifstream write;
	write.open(fileName);
	noSentences=0;
}

void TextBuddy::Display()
{       
	cout<<endl;
	if(paragraph.empty()==true)
		cout<<fileName<<isEmpty<<endl;
	for(int i=0;i<paragraph.size();i++)
	{
		cout<<i+1<<"."<<paragraph[i]<<endl;
	}
	cout<<endl;
}

//overloaded function printing paragraph
void TextBuddy::Display(vector<string>paragraph,vector<int>number)
{       
	cout<<endl;
	if(paragraph.empty()==true)
		cout<<fileName<<isEmpty<<endl;
	for(int i=0;i<paragraph.size();i++)
	{
		cout<<number[i]<<"."<<paragraph[i]<<endl;
	}
	cout<<endl;
}


void TextBuddy::UpdateFile()
{   ofstream write;
write.open(fileName);
for(int i=0;i<paragraph.size();i++)
{
	write<<paragraph[i]<<endl;
}
write.close();
}

//removes spaces and tab spaces from the end of a string
void TextBuddy::TrimString(string& str)
{
	while(str[0]==' ' ||str[0]=='\t'||str[0]=='\n')
	{
		str=str.substr(1);
	}
	while(str[str.size()-1]==' '||str[str.size()-1]=='\t'||str[str.size()-1]=='\n')
	{
		str=str.substr(0,str.size()-1);
	}
}

void TextBuddy::DeleteSentence(int& sentenceNumber)
{
	if(sentenceNumber==0 || sentenceNumber>paragraph.size())
	{
		return;
	}
	cout<<deletedFrom<< fileName<<" "<<paragraph[sentenceNumber-1]<<endl<<endl;
	paragraph.erase(paragraph.begin()+sentenceNumber-1);


}

// The vector<string> return type is only provided for Unit Testing using the Google Test Framework. This is a CASE INSENSITIVE search
string TextBuddy::Search(string searchString)
{   
	vector<string>sentenceFound;
	vector<int>sentenceNumber;
	int flagFound=0;
	TrimString(searchString);
	for(int i=0;i<paragraph.size();i++)
	{   string dummy=paragraph[i];
	flagFound=CaseInsensitiveSearch(dummy,searchString);
	if(flagFound==1)
	{   sentenceFound.push_back(paragraph[i]);
	sentenceNumber.push_back(i+1);
	flagFound=0;
	}


	}

	if(sentenceFound.empty())
	{
		cout<<searchUnsuccessful<<endl;
		return "";
	}
	else
	{
		cout<<searchString<<foundString<<endl;
		Display(sentenceFound,sentenceNumber);

	}

	return ConvertVectorToString(sentenceFound);

}
int TextBuddy::ConvertStringToInt(string input)
{   string dummy;
istringstream x(input);
int sentenceNumber;
x>>dummy;
x>>sentenceNumber;
return sentenceNumber;
}
int TextBuddy::CaseInsensitiveSearch(string a,string b)
{
	string cpy1=a;
	string cpy2=b;
	size_t found;

	std::transform(cpy1.begin(), cpy1.end(), cpy1.begin(), ::tolower);
	std::transform(cpy2.begin(), cpy2.end(), cpy2.begin(), ::tolower);
	found=cpy1.find(cpy2);
	if(found!=string::npos)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
string TextBuddy::ConvertVectorToString(vector<string> str)
{
	string strFinal="";
	for(int i=0;i<str.size();i++)
	{		
		strFinal+=str[i]+" ";
	}
	return strFinal.substr(0,strFinal.size()-1);
}
string TextBuddy::Sort()
{
	sort(paragraph.begin(),paragraph.end());
	string result=ConvertVectorToString(paragraph);
	cout<<sortCompleted<<endl;
	return result;
}

//Function converts a number from string to int type for use by OperationHandler(...)



void  TextBuddy::OperationHandler(string& input,string& addend,string& command)
{    int sentenceNumber;
if(stricmp(command.c_str(),addString.c_str())==0)
{  TrimString(addend);
Add(addend);
}
else if(stricmp(command.c_str(),clearString.c_str())==0)
{
	Clear();
}
else if(stricmp(command.c_str(),displayString.c_str())==0)
{
	Display();
}
else if(stricmp(command.c_str(),searchString.c_str())==0)
{
	string dummy=Search(addend);
}
else if(stricmp(command.c_str(),sortString.c_str())==0)
{
	Sort();
}
else if(stricmp(command.c_str(),deleteString.c_str())==0)
{
	sentenceNumber=ConvertStringToInt(input);
	DeleteSentence(sentenceNumber);
}
else if(stricmp(command.c_str(),exitString.c_str())==0)
{ 
	UpdateFile();
	exit(1);
}

else 
{
	cout<<incorrectCommand<<endl;
}
}



void TextBuddy::Execute()
{
	string input,command,addend;
	DisplayHeaderText();
	while(true)
	{   
		cout<<commandString;
		getline(cin,input);
		if(input[0]==' ')
		{
			TrimString(input);
		}
		input+=" ";
		for(int i=0;i<input.length();i++)
			if(input[i]==' ')
			{	
				command=input.substr(0,i);
				addend=input.substr(i);
				break;
			}

			OperationHandler(input,addend,command);

	}
	return ;
}

int main(int argc,char* argv[])
{	
	//Running Unit Tests
	runTest(argc,argv);


	TextBuddy newText;
	newText.SetName(argv[1]);
	newText.Execute();

	return 0;


}

